﻿using UnityEngine;
using System.Threading.Tasks;

public class SyncCallAsyncWithCallback : MonoBehaviour
{
    private void Start()
    {
        // 同步方法调用异步方法，并传入回调函数
        CallAsyncMethodWithCallback(OnAsyncTaskCompleted);
    }

    private void CallAsyncMethodWithCallback(System.Action<string> callback)
    {
        // 启动异步任务
        AsyncMethod().ContinueWith(task =>
        {
            if (task.IsCompletedSuccessfully)
            {
                // 异步任务完成，调用回调函数
                callback(task.Result);
            }
            else if (task.IsFaulted)
            {
                Debug.LogError("异步任务出错: " + task.Exception);
            }
        });
    }

    private async Task<string> AsyncMethod()
    {
        // 模拟耗时操作
        await Task.Delay(10000);
        return "异步任务完成";
    }

    private void OnAsyncTaskCompleted(string result)
    {
        Debug.Log("异步任务结果: " + result);
    }

    void Update()
    {
        Debug.Log("Update" );
    }
}